package config

import (
	"fmt"
	"path/filepath"

	"torler.com/qingyue/pkg/email"
	"torler.com/qingyue/pkg/env"
	"torler.com/qingyue/pkg/jwt"
	"torler.com/qingyue/pkg/logger"
	"torler.com/qingyue/pkg/storage"
)

type Config struct {
	HTTP     *HTTPConfig          `mapstructure:"http"`
	Security *jwt.JwtConfig       `mapstructure:"security"`
	Log      *logger.LogConfig    `mapstructure:"log"`
	DB       *storage.DBConfig    `mapstructure:"db"`
	Mail     email.SmtpConfig     `mapstructure:"mail"`
	Redis    *storage.RedisConfig `mapstructure:"redis"`
}

// HTTP 配置
type HTTPConfig struct {
	Host string `mapstructure:"host"`
	Port int    `mapstructure:"port"`
}

var conf Config

func LoadConfig() error {
	name := fmt.Sprintf("%s.yml", env.GetMode())
	var cfg Config
	configPath := filepath.Join(env.GetConfigDir(), name)
	err := env.LoadFromFile(&cfg, configPath)
	if err != nil {
		return err
	}
	conf = cfg
	return nil
}

func GetConfig() *Config {
	return &conf
}
